const express = require('express');
const cors = require('cors');
const path = require('path');
const swaggerConfig = require('./src/config/swagger');

// 这里初始化 app
const app = express();

// 导入路由
const userRoutes = require('./src/routes/userRoutes');
const homeRoutes = require('./src/routes/homeRoutes'); // 新增首页路由

// 导入passport
const passport = require('passport');

// 中间件配置
app.use(cors({
  origin: 'http://localhost:8080',
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
  allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
  credentials: true
}));

// 配置静态文件服务
app.use('/images', express.static(path.join(__dirname, 'images')));

// 强制使用JSON解析中间件
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));

// 使用JWT认证中间件
app.use(passport.initialize());

// Swagger 配置 
swaggerConfig(app);

// 常规路由
app.use('/api/auth', require('./src/routes/authRoutes'));
// 用户路由
app.use('/api/user', require('./src/routes/userRoutes'));
// 首页路由
app.use('/api/home', homeRoutes);
// 搜索路由
app.use('/api/search', require('./src/routes/searchRoutes'));
// 发帖路由
app.use('/api/posts', require('./src/routes/postRoutes'));
// 评论路由
app.use('/api/comments', require('./src/routes/commentRoutes'));
// 好友路由
app.use('/api/friend', require('./src/routes/friendRoutes'));
// 聊天路由
app.use('/api/chat', require('./src/routes/chatRoutes'));
// 首页数据路由
app.use('/api/home', require('./src/routes/homeRoutes')); // 仅保留这一个首页路由
// 系统通知路由
app.use('/api/system-message', require('./src/routes/systemMessageRoutes'));

// 启动服务器
module.exports = app;